package com.atguigu.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.vo.chapter.ChapterVo;
import com.atguigu.eduservice.entity.vo.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2024-06-23
 */
@Service
@Slf4j
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduVideoService eduVideoService;
    //返回章节大纲
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //1.更具课程id查询课程里面的所有章节
        QueryWrapper<EduChapter> eduChapterWrapper = new QueryWrapper<>();
        eduChapterWrapper.eq("course_id",courseId);
        List<EduChapter> eduChapters = baseMapper.selectList(eduChapterWrapper);




        //2.根据课程id查询课程里面的所有小节
        QueryWrapper<EduVideo> eduVideoWrapper = new QueryWrapper<>();
        eduChapterWrapper.eq("course_id",courseId);
        List<EduVideo> eduVideos = eduVideoService.list(eduVideoWrapper);

        //3.将小结和章节封装到一个集合中去中
//        List<ChapterVo> chapterVideoByCourse = new ArrayList<>();
//        for (int i = 0; i < eduChapters.size(); i++) {
//            //获取每个章节
//            ChapterVo chapterVo = new ChapterVo();
//            EduChapter eduChapter = eduChapters.get(i);
//            BeanUtils.copyProperties(eduChapter,chapterVo);
//            chapterVideoByCourse.add(chapterVo);
//            List<VideoVo> videoVoList = new ArrayList<>();
//            for (int j = 0; j < eduVideos.size(); j++) {
//                EduVideo eduVideo = eduVideos.get(j);
//                if(eduVideo.getChapterId().equals(eduChapter.getId())){
//                    VideoVo videoVo = new VideoVo();
//                    BeanUtils.copyProperties(eduVideo,videoVo);
//                    videoVoList.add(videoVo);
//                }
//            }
//            chapterVo.setChildren(videoVoList);
//        }
        List<ChapterVo> chapterVideoByCourse = eduChapters.stream()
                .map(eduChapter -> {
                    ChapterVo chapterVo = new ChapterVo();
                    BeanUtils.copyProperties(eduChapter, chapterVo);

                    List<VideoVo> videoVoList = eduVideos.stream()
                            .filter(eduVideo -> eduVideo.getChapterId().equals(eduChapter.getId()))
                            .map(eduVideo -> {
                                VideoVo videoVo = new VideoVo();
                                BeanUtils.copyProperties(eduVideo, videoVo);
                                return videoVo;
                            })
                            .collect(Collectors.toList());

                    chapterVo.setChildren(videoVoList);
                    return chapterVo;
                })
                .collect(Collectors.toList());
        log.info("chapterVideoByCourse:{}",eduChapters);
        log.info("eduChapters:{}",eduVideos);
        return chapterVideoByCourse;
    }


    //删除章节
    @Override
    public boolean deleteChapter(String chapterId) {
        //根据章节id 查询出小节
        QueryWrapper<EduVideo> eduChapterQueryWrapper = new QueryWrapper<>();
        eduChapterQueryWrapper.eq("chapter_id",chapterId);
        //判断是否章节下是否有小结
        int count = eduVideoService.count(eduChapterQueryWrapper);
        //如果大于0则提示不能删除
        if(count>0){
            throw new GuliException(20001,"不能删除");
        }
            //如果没有则删除，如果返回值大于0则删除成功！
            int i = baseMapper.deleteById(chapterId);
            return i>0;
    }
    //根据课程id删除章节
    @Override
    public void removeChapterCourseId(String courseId) {
        QueryWrapper<EduChapter> eduChapterQueryWrapper = new QueryWrapper<>();
        eduChapterQueryWrapper.eq("course_id",courseId);
        baseMapper.delete(eduChapterQueryWrapper);
    }
}
